package com.example.triprec.ui.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.triprec.R;
import com.example.triprec.common.Result;
import com.example.triprec.dao.CommentDao;
import com.example.triprec.entity.Comment;
import com.example.triprec.entity.User;
import com.example.triprec.ui.adapter.MyCommentAdapter;
import com.example.triprec.utils.CurrentUserUtils;

import java.util.ArrayList;
import java.util.List;

public class MyCommentsActivity extends AppCompatActivity {

    private ImageView ivBack;
    private RecyclerView rvComments;
    private LinearLayout layoutEmpty;

    private MyCommentAdapter commentAdapter;
    private List<Comment> commentList = new ArrayList<>();
    private User currentUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_comments);

        initViews();
        setupListeners();
        initData();
    }

    private void initViews() {
        ivBack = findViewById(R.id.iv_back);
        rvComments = findViewById(R.id.rv_comments);
        layoutEmpty = findViewById(R.id.layout_empty);

        // 设置RecyclerView
        rvComments.setLayoutManager(new LinearLayoutManager(this));
        commentAdapter = new MyCommentAdapter(commentList);
        rvComments.setAdapter(commentAdapter);
    }

    private void setupListeners() {
        // 返回按钮点击事件
        ivBack.setOnClickListener(v -> finish());

        // 评论删除点击事件
        commentAdapter.setOnItemClickListener(new MyCommentAdapter.OnItemClickListener() {
            @Override
            public void onDeleteClick(Comment comment, int position) {
                showDeleteConfirmDialog(comment, position);
            }
        });
    }

    private void initData() {
        // 获取当前登录用户
        try {
            currentUser = CurrentUserUtils.getCurrentUser();
            loadComments();
        } catch (Exception e) {
            Toast.makeText(this, "获取用户信息失败", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private void loadComments() {
        Result<List<Comment>> result = CommentDao.getCommentsByUserId(currentUser.getId());
        if (result.isSuccess()) {
            commentList.clear();
            if (result.getData() != null && !result.getData().isEmpty()) {
                commentList.addAll(result.getData());
                layoutEmpty.setVisibility(View.GONE);
                rvComments.setVisibility(View.VISIBLE);
            } else {
                layoutEmpty.setVisibility(View.VISIBLE);
                rvComments.setVisibility(View.GONE);
            }
            commentAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    private void showDeleteConfirmDialog(Comment comment, int position) {
        new AlertDialog.Builder(this)
                .setTitle("确认删除")
                .setMessage("确定要删除这条评论吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteComment(comment, position);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void deleteComment(Comment comment, int position) {
        Result<Void> result = CommentDao.deleteComment(comment.getId());
        if (result.isSuccess()) {
            commentList.remove(position);
            commentAdapter.notifyItemRemoved(position);
            commentAdapter.notifyItemRangeChanged(position, commentList.size() - position);

            // 检查是否所有评论都已删除
            if (commentList.isEmpty()) {
                layoutEmpty.setVisibility(View.VISIBLE);
                rvComments.setVisibility(View.GONE);
            }

            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
} 